/*
 * @(#)EcsApi.java   1.0  2021年9月13日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.register.alclb.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.aliyuncs.ecs.model.v20140526.DescribeInstancesRequest;
import com.aliyuncs.ecs.model.v20140526.DescribeInstancesResponse;
import com.aliyuncs.ecs.model.v20140526.DescribeInstancesResponse.Instance;
import com.aliyuncs.ecs.model.v20140526.DescribeInstancesResponse.Instance.NetworkInterface;
import com.beust.jcommander.internal.Lists;
import com.swift.api.aliyun.client.EcsClient;
import com.swift.exception.extend.SystemException;
import com.swift.util.type.JsonUtil;
import com.swift.util.type.TypeUtil;

/**
 * 添加说明 
 * @author zhengjiajin
 * @version 1.0 2021年9月13日
 */
@Component
public class EcsApi {
    @Autowired
    private EcsClient ecsClient;
    /**
     * 得到实倒详情
     * @param instanceId
     * @return
     */
    public Instance getInfo(String instanceId) {
        try {
            DescribeInstancesRequest req = new DescribeInstancesRequest();
            req.setInstanceIds(JsonUtil.toJson(Lists.newArrayList(instanceId)));
            DescribeInstancesResponse resecs = ecsClient.getClient().getAcsResponse(req);
            if(resecs==null) return null;
            if(TypeUtil.isNull(resecs.getInstances())) return null;
            return resecs.getInstances().get(0);
        }catch (Exception e) {
            throw new SystemException("请求阿里服务器API异常：",e);
        }
    }
    /**
     * 得到内网IP
     * @param instanceId
     * @return
     */
    public String getIp(Instance instance) {
        if(TypeUtil.isNull(instance.getNetworkInterfaces())) return null;
        for(NetworkInterface networkInterface : instance.getNetworkInterfaces()) {
            return networkInterface.getPrimaryIpAddress();
        }
        return null;
    }
    
    /**
     * 通过内网IP查询服务器实倒详情
     * @param ip
     * @return
     */
    public Instance getEcsInstanceId(String ip) {
        Instance instance = getEcsInstanceId("vpc", ip);
        if(TypeUtil.isNotNull(instance)) return instance;
        return getEcsInstanceId("classic", ip);
    }
    
    private Instance getEcsInstanceId(String networkType, String ip) {
        try {
            DescribeInstancesRequest req = new DescribeInstancesRequest();
            req.setInstanceNetworkType(networkType);
            if(networkType.equals("vpc")) req.setPrivateIpAddresses(JsonUtil.toJson(Lists.newArrayList(ip)));
            else req.setInnerIpAddresses(JsonUtil.toJson(Lists.newArrayList(ip)));
            DescribeInstancesResponse resecs = ecsClient.getClient().getAcsResponse(req);
            if(resecs==null) return null;
            if(TypeUtil.isNull(resecs.getInstances())) return null;
            for(Instance instance : resecs.getInstances()) {
                if(TypeUtil.isNull(instance.getNetworkInterfaces())) continue;
                for(NetworkInterface networkInterface : instance.getNetworkInterfaces()) {
                    if(ip.equals(networkInterface.getPrimaryIpAddress())) return instance;
                }
            }
            return null;
        }catch (Exception e) {
            throw new SystemException("请求阿里服务器API异常：",e);
        }
    }
}
